package com.freejy.ecommunity.web.controller;


import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.freejy.ecommunity.model.Topic;
import com.freejy.ecommunity.service.TopicService;

@RestController
public class TopicController extends BaseController{

    @Autowired
    private TopicService topicService;

    /**
     * 分页获取全部标签列表
     * @return
     */
    @RequestMapping("/getTopics")
    public ResponseEntity<?> getTopics(@Valid @RequestBody String  info){
    	JSONObject json=JSONObject.parseObject(info);
        return success(topicService.getTopics(json.getInteger("size"), json.getInteger("limit")));
    }

    /**
     * 根据板块id分页获取帖子信息
     * @return
     */
    @RequestMapping("/getTopicsByPlateId")
    public ResponseEntity<?> getTopicsByPlateId(@Valid @RequestBody String  info){
    	JSONObject json=JSONObject.parseObject(info);
        return success(topicService.getTopicsByPlateId(json.getInteger("plateId"),json.getInteger("size") ,json.getInteger("limit") ));
    }
    
    /**
     * 新增帖子
     * @return
     */
    @RequestMapping("/insertTopic")
    public ResponseEntity<?> insertTopic(@Valid @RequestBody String  info){
    	JSONObject json=JSONObject.parseObject(info);
    	Topic topic = (Topic) JSONObject.toJavaObject(json, Topic.class);
        return success(Integer.valueOf(topicService.insert(topic)));
    }
    
    /**
     * 新增帖子
     * @return
     */
    @RequestMapping("/updateTopic")
    public ResponseEntity<?> updateTopic(@Valid @RequestBody String  info){
    	JSONObject json=JSONObject.parseObject(info);
    	Topic topic = (Topic) JSONObject.toJavaObject(json, Topic.class);
        return success(Integer.valueOf(topicService.updateById(topic)));
    }
    
}
